/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.arquimedes.jms;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

/**
 *
 * @author juanda
 */
public class ListenerJMS implements MessageListener {

    @Resource(mappedName = "jms/javaee6/ConnectionFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(mappedName = "jms/javaee6/Topic")
    private static Topic topic;

    public static void main(String[] args) throws JMSException {
// Creates the needed artifacts to connect to the queue
	Connection connection = connectionFactory.createConnection();
	Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	MessageConsumer consumer = session.createConsumer(topic);
	consumer.setMessageListener(new ListenerJMS());
	connection.start();
    }

    @Override
    public void onMessage(Message message) {
	try {
	    System.out.println("Message received: " + ((TextMessage) message).getText());
	} catch (JMSException ex) {
	    Logger.getLogger(ListenerJMS.class.getName()).log(Level.SEVERE, null, ex);
	}
    }
}
